package com.supervision.parent.ui.notifications;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.supervision.parent.R;
import com.supervision.parent.util.SaveNum;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URL;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NotificationsFragment extends Fragment {

    private NotificationsViewModel notificationsViewModel;

    String phone, webIp;
    private LinearLayout llStu;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                ViewModelProviders.of(this).get(NotificationsViewModel.class);
        View root = inflater.inflate(R.layout.fragment_notifications, container, false);
        final TextView textView = root.findViewById(R.id.tv_phone);
        llStu = root.findViewById(R.id.ll_stu);


        final Map<String, String> userInfo = SaveNum.getUserInfo(getContext());
        webIp = userInfo.get("webIp");
        phone = userInfo.get("phone");
        textView.setText(phone);
        StuInfoAT stuInfoAT = new StuInfoAT();
        stuInfoAT.execute(webIp, phone);

        notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {

            }
        });
        return root;
    }

    class StuInfoAT extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
            OkHttpClient okHttpClient = new OkHttpClient();//创建单例
            URL url;
            Request request;//创建请求
            String result = null;
            try {
                //设置url
                url = new URL("http://" + strings[0] + ":8080/getStudent/" + strings[1]);
                System.out.println(url);
                //设置请求
                request = new Request.Builder()
                        .url(url)
                        .build();
                //执行请求
                Response response = okHttpClient.newCall(request).execute();

                result = response.body().string();
            } catch (Exception e) {
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            JSONArray jsonArray = null;
            try {
                jsonArray = new JSONArray(s);
                for (int i = 0; i < jsonArray.length(); i++) {
                    System.out.println(2);
                    JSONObject jsonObject = new JSONObject(jsonArray.get(i).toString());
                    TextView textView = new TextView(getContext());
                    textView.setText(jsonObject.optString("name") +
                            "   " + jsonObject.getString("sex") +
                            "   班级：" + jsonObject.getString("clazz"));
                    textView.setTextSize(25);
                    llStu.addView(textView);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            System.out.println(5);
        }

    }
}
